// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System.Data.Common;
using MySql.Data.MySqlClient;

namespace SnapObjects.Data.MySql
{
    /// <summary>
    ///     Represents the entry to the MySql database.
    /// </summary>
    public class MySqlDataContext : DataContext
    {
        /// <summary>
        ///     Initializes a new instance of the MySqlDataContext class by passing the MySqlDataContextOptions type
        ///     object as a parameter.
        /// </summary>
        /// <param name="dataContextOptions">The options to be set for the DataContext object.</param>
        public MySqlDataContext(IDataContextOptions dataContextOptions) :
            base(dataContextOptions)
        {
            this.AdoDbProviderFactory = new MySqlAdoDbProviderFactory(this);
            this.AdoDbSqlProvider = new MySqlAdoDbSqlProvider(this.ContextOptions);
            this.AdoDbSchemaProvider = new MySqlAdoDbSchemaProvider(this);
        }

        /// <summary>
        ///     Initializes a new instance of the MySqlDataContext class by passing the MySqlConnection type
        ///     object as a parameter.
        /// </summary>
        /// <param name="dbConnection">The connection object which is a MySqlConnection instance.</param>
        public MySqlDataContext(MySqlConnection dbConnection) :
            base(dbConnection, new MySqlDataContextOptions(System.String.Empty))
        {
            this.AdoDbProviderFactory = new MySqlAdoDbProviderFactory(this);
            this.AdoDbSqlProvider = new MySqlAdoDbSqlProvider(this.ContextOptions);
            this.AdoDbSchemaProvider = new MySqlAdoDbSchemaProvider(this);
        }

        internal override IAdoDbProviderFactory AdoDbProviderFactory { get; }

        internal override IAdoDbSchemaProvider AdoDbSchemaProvider { get; }

        internal override IAdoDbSqlProvider AdoDbSqlProvider { get; }

        internal override IAdoDbConnection CreateAdoDbConnection(string connectionString)
        {
            return new MySqlAdoDbConnection(connectionString);
        }

        internal override IAdoDbConnection CreateAdoDbConnection(DbConnection dbConnection)
        {
            return new MySqlAdoDbConnection((MySqlConnection)dbConnection);
        }
    }
}
